package com.xvaca.spcms.servlet.report;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xvaca.spcms.bean.SettlementBean;
import com.xvaca.spcms.dao.OrderDAO;
import com.xvaca.spcms.dao.PipeDAO;

/**
 * Servlet implementation class SettlementShowServlet
 */
public class SettlementShowServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String currentDate = sdf.format(new Date());
		
		request.setAttribute("start_at", currentDate);
		request.setAttribute("end_at", currentDate);
		
		PipeDAO pipeDao = new PipeDAO();
		request.setAttribute("pipes", pipeDao.getPipeList());
		request.getRequestDispatcher("settlement_show.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String pipeId = request.getParameter("pipe_id");
		String startAt = request.getParameter("start_at");
		String endAt = request.getParameter("end_at");
		
		OrderDAO dao = new OrderDAO();
		List<SettlementBean> list = dao.getSettlementDateByPipes(startAt, endAt, Integer.parseInt(pipeId));
		request.setAttribute("list", list);
		
		PipeDAO pipeDao = new PipeDAO();
		request.setAttribute("pipes", pipeDao.getPipeList());
		
		request.setAttribute("pipe_id", pipeId);
		request.setAttribute("start_at", startAt);
		request.setAttribute("end_at", endAt);
		request.getRequestDispatcher("settlement_show.jsp").forward(request, response);
		
	}

}
